/**
 * 
 */
package com.yidu.drug.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.InventoryModelMapper;
import com.yidu.drug.dao.InventoryRecordModelMapper;
import com.yidu.drug.dao.QtSumModelMapper;
import com.yidu.drug.dao.QualityTestingModelMapper;
import com.yidu.drug.dao.RepertoryModelMapper;
import com.yidu.drug.model.InventoryModel;
import com.yidu.drug.model.InventoryRecordModel;
import com.yidu.drug.model.QtSumModel;
import com.yidu.drug.model.QualityTestingModel;
import com.yidu.drug.model.RepertoryModel;
import com.yidu.drug.service.RepertoryService;

/**
 * 库存Service实现类
 * @author tanglei
 * @date 2018年8月20日 上午9:48:30
 * @version 1.0
 */
@Service
public class RepertoryServiceImpl implements RepertoryService{
	//注入库存Dao
	@Resource
	private RepertoryModelMapper repertoryDao;

	//入库明细dao
	@Resource
	private InventoryModelMapper inventoryDao;

	//入库记录dao
	@Resource
	private InventoryRecordModelMapper inventoryRecordDao;
	
	@Resource
	private QualityTestingModelMapper qualityTestingDao;

	@Override
	public int deleteByPrimaryKey(String repId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(RepertoryModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertSelective(RepertoryModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public RepertoryModel selectByPrimaryKey(String repId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(RepertoryModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateByPrimaryKey(RepertoryModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteByPwId(String pwId, Long repNumber) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 根据分公司ID查询库存数据
	 * @param repertory 传入一个库存对象
	 * @return 返回一个 map集合
	 */
	@Override
	public Map<String, Object> findAllDrugOrMaterials(RepertoryModel record) {
		//创建一个分页对象
		Page page = new Page();
		//给分页对象里面的页数属性赋值，数值为库存对象里面的页数数值
		page.setPage(Integer.valueOf(record.getPage()));
		//给分页对象里面的每页显示行数的属性赋值，数值为库存对象里面的每页显示行数数值
		page.setRows(Integer.valueOf(record.getLimit()));
		//给库存里面的分页对象属性赋值
		record.setPages(page);
		//创建一个map集合
		Map<String,Object> map = new HashMap<String,Object>();
		//创建一个list集合
		List<RepertoryModel> list = new ArrayList<RepertoryModel>();
		//定义一个count变量
		Long count = (long)0;
		//判断，判断是查询药品库存还是查询原材料库存
		if("drug".equals(record.getCondition())){
			//调用库存Dao里面的查询药品库存的方法
			list = repertoryDao.findByFileIdDrugRepertory(record);
			//调用库存Dao里面的查询药品库存总行数的方法
			count = repertoryDao.findDrugRepertoryRows(record);
		}else if("materials".equals(record.getCondition())){
			//调用库存Dao里面的查询原材料库存的方法
			list = repertoryDao.findByFileIdMaterialsRepertory(record);
			//调用库存Dao里面的查询原材料库存总行数的方法
			count = repertoryDao.findMaterialsRepertoryRows(record);
		}
		//给map集合赋值
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", list);
		//返回map集合
		return map;
	}

	/**
	 * 根据分公司ID查询物品名称和物品类型
	 * @param repertory 传入一个库存对象
	 * @return 返回一个list集合
	 */
	@Override
	public List<RepertoryModel> findAllItemNameAndItemType(RepertoryModel record) {
		//创建一个list集合
		List<RepertoryModel> list = new ArrayList<RepertoryModel>();
		//判断，判断是查询药品名称，药品类型名称还是查询原材料名称，原材料类型名称
		if("drug".equals(record.getCondition())){
			//调用库存Dao里面的查询药品名称和药品类型名称的方法
			list = repertoryDao.findDrugNameAndDrugType(record);
		}else if("materials".equals(record.getCondition())){
			//调用库存Dao里面的查询原材料名称和原材料类型名称的方法
			list = repertoryDao.findMaterialsNameAndMaterialType(record);
		}
		//循环遍历集合,去重复
		for(int i=0;i<list.size();i++){
			for(int j=list.size()-1;j>i;j--){
				if(list.get(i).getItemType().equals(list.get(j).getItemType())){
					list.get(j).setItemType("");
				}
			}
		}
		//返回list集合
		return list;
	}

	@Override
	public RepertoryModel selectByFilId(String filId) {
		// TODO Auto-generated method stub
		return repertoryDao.selectByFilId(filId);
	}

	@Override
	public void update(RepertoryModel repertory) {
		repertoryDao.updateByPrimaryKey(repertory);

	}

	/**
	 * 根据分公司id查询库存id
	 */
	@Override
	public String findById(String filId) {

		return repertoryDao.findById(filId);
	}

	/**
	 * 根据分公司id修改库存表数量
	 */
	@Override
	public Object updates(Long repNumber, String filId) {

		return repertoryDao.updates(repNumber, filId);
	}


	@Override
	public List<RepertoryModel> insertById(QtSumModel qtSumModel) {
		QualityTestingModel qt = new QualityTestingModel();
		
		//获取当前日期时间
		Timestamp ss = new Timestamp(new Date().getTime());
		
		RepertoryModel repertory = new RepertoryModel();
		System.out.println(qtSumModel.getFilId()+"=============>"+qtSumModel.getSumId());
		repertory.setSumId(qtSumModel.getSumId());
		repertory.setFilId(qtSumModel.getFilId());
		List<RepertoryModel> list = repertoryDao.selectById(repertory);
		List<QualityTestingModel> listOne = qualityTestingDao.selectByPrimaryKeyTwo(qtSumModel.getSumId());
		for(RepertoryModel repertoryModel:list){
			System.out.println(repertoryModel.getPwId()+"========================");
			for(QualityTestingModel qualityTestingModel:listOne){
				if(repertoryModel.getPwId() == qualityTestingModel.getPwId()){
					System.out.println(repertoryModel.getPwId()+"---------->"+qualityTestingModel.getPwId());
					
					//修改库存表数量方法
					repertoryDao.updateById(repertory);
				}else {
					System.out.println("哈哈");
					
					//增加库存表
					String repId = UUID.randomUUID().toString();
					repertory.setRepId(repId);
					repertory.setFilId(qtSumModel.getFilId());
					repertory.setOptime(ss);
					//数量
					//repertory.setRepNumber(((double)qtSumModel.getSumSumnum()-(double)qtSumModel.getSumNopass())+(long)repertory.getRepNumber());
					//操作人
					repertory.setOper(qtSumModel.getSumPerson());
					//是否有效
					repertory.setIsva(Tools.ISVA_YES);
					//排序
					repertory.setSort(qtSumModel.getSort());
					//业务id
					repertory.setPwId(qt.getPwId());
					
					//增加库存表
					repertoryDao.insertSelective(repertory);
					
					
					
					
					
					//增加库存明细方法
					InventoryModel it = new InventoryModel();
					
					String invId=UUID.randomUUID().toString();
					it.setInvId(invId);
					it.setRepId(repertory.getRepId());
					//数量
					//it.setInvNumber(new Long(repertory.getRepNumber()).intValue());
					it.setOperator("管理员");
					
					it.setCztime(ss);
					//是否有效
					it.setIsva(Tools.ISVA_YES);
					//排序
					it.setSort(qtSumModel.getSort());
					//批次号码
					Integer sj=Tools.getRandomNumber(10000);
					//得到当前时间
					Timestamp t = new Timestamp(new Date().getTime());
					//批次号
					it.setNumbers("批:"+t+sj);
					//生产日期
					it.setProDate(ss);
					
					//增加库存明细方法
					inventoryDao.insertSelective(it);
					
					
					
					
					
					//增加入库记录方法
					InventoryRecordModel ir = new InventoryRecordModel();
					String irId=UUID.randomUUID().toString();
					//库存记录表
					ir.setIrId(irId);
					//业务ID
					ir.setPwId(repertory.getPwId());
					
					//药材数量
					//ir.setMatNum(new Long(repertory.getRepNumber()).intValue());
					//记录时间
					//获取当前日期时间
					ir.setIrTime(ss);
					
					//记录备注
					ir.setIrRemark("已记录");
					//操作人
					ir.setOperator(it.getOperator());
					//是否有效
					ir.setIsva(Tools.ISVA_YES);
					//排序
					ir.setSort(qtSumModel.getSort());
					//分公司ID
					ir.setFilId(repertory.getFilId());
					ir.setNumbers(it.getNumbers());
					
					
					//增加入库记录方法
					inventoryRecordDao.insertSelective(ir);
					
					
					
				}
			}
		}
		
		return null;
		/*//判断库存数量是否有数据
		if(list!=null){

			//修改库存表数量方法
			repertoryDao.updateById(repertory);
			
			
			InventoryModel it = new InventoryModel();
			String invId=UUID.randomUUID().toString();
			it.setInvId(invId);
			it.setRepId(repertory.getRepId());
			//数量
			it.setInvNumber(new Long(repertory.getRepNumber()).intValue());
			it.setOperator("管理员");
			
			it.setCztime(ss);
			//是否有效
			it.setIsva(Tools.ISVA_YES);
			//排序
			it.setSort(qtSumModel.getSort());
			//批次号码
			Integer sj=Tools.getRandomNumber(10000);
			//得到当前时间
			Timestamp t = new Timestamp(new Date().getTime());
			//批次号
			it.setNumbers("批:"+t+sj);
			//生产日期
			it.setProDate(ss);
			
			//增加库存明细方法
			inventoryDao.insertSelective(it);
			
			

			InventoryRecordModel ir = new InventoryRecordModel();
			String irId=UUID.randomUUID().toString();
			//库存记录表
			ir.setIrId(irId);
			//业务ID
			ir.setPwId(repertory.getPwId());
			
			//药材数量
			ir.setMatNum(new Long(repertory.getRepNumber()).intValue());
			//记录时间
			//获取当前日期时间
			ir.setIrTime(ss);
			
			//记录备注
			ir.setIrRemark("已记录");
			//操作人
			ir.setOperator(it.getOperator());
			//是否有效
			ir.setIsva(Tools.ISVA_YES);
			//排序
			ir.setSort(qtSumModel.getSort());
			//分公司ID
			ir.setFilId(repertory.getFilId());
			ir.setNumbers(it.getNumbers());
			
			
			//增加入库记录方法
			inventoryRecordDao.insertSelective(ir);
		
		
		
		
		
		}else{//没有数据
			
			//增加库存表
			repertoryDao.insertSelective(repertory);
			String repId = UUID.randomUUID().toString();
			
			repertory.setRepId(repId);
			repertory.setFilId(qtSumModel.getFilId());
			repertory.setOptime(ss);
			//数量
			repertory.setRepNumber((qtSumModel.getSumSumnum()-qtSumModel.getSumNopass())+repertory.getRepNumber());
			//操作人
			repertory.setOper(qtSumModel.getSumPerson());
			//是否有效
			repertory.setIsva(Tools.ISVA_YES);
			//排序
			repertory.setSort(qtSumModel.getSort());
			//业务id
			repertory.setPwId(qt.getPwId());
			
			
			InventoryModel it = new InventoryModel();
			String invId=UUID.randomUUID().toString();
			it.setInvId(invId);
			it.setRepId(repertory.getRepId());
			//数量
			it.setInvNumber(new Long(repertory.getRepNumber()).intValue());
			it.setOperator("管理员");
			
			it.setCztime(ss);
			//是否有效
			it.setIsva(Tools.ISVA_YES);
			//排序
			it.setSort(qtSumModel.getSort());
			//批次号码
			Integer sj=Tools.getRandomNumber(10000);
			//得到当前时间
			Timestamp t = new Timestamp(new Date().getTime());
			//批次号
			it.setNumbers("批:"+t+sj);
			//生产日期
			it.setProDate(ss);
			
			//增加库存明细方法
			inventoryDao.insertSelective(it);
			
			
			InventoryRecordModel ir = new InventoryRecordModel();
			String irId=UUID.randomUUID().toString();
			//库存记录表
			ir.setIrId(irId);
			//业务ID
			ir.setPwId(repertory.getPwId());
			
			//药材数量
			ir.setMatNum(new Long(repertory.getRepNumber()).intValue());
			//记录时间
			//获取当前日期时间
			ir.setIrTime(ss);
			
			//记录备注
			ir.setIrRemark("已记录");
			//操作人
			ir.setOperator(it.getOperator());
			//是否有效
			ir.setIsva(Tools.ISVA_YES);
			//排序
			ir.setSort(qtSumModel.getSort());
			//分公司ID
			ir.setFilId(repertory.getFilId());
			ir.setNumbers(it.getNumbers());
			
			
			//增加入库记录方法
			inventoryRecordDao.insertSelective(ir);
		
		}
		return list;

*/
	}

	@Override
	public List<RepertoryModel> selectById(RepertoryModel record) {
		return repertoryDao.selectById(record);
	}

	@Override
	public String updateById(RepertoryModel record) {

		//定义rows 运行修改方法
		int rows = repertoryDao.updateById(record);
		//if判断rows>0时进入页面上的ok程序
		if(rows > 0){
			return "ok";
		}else {
			return "no";
		}

	}
	/**
	 * 王志伟根据业务ID查询库存
	 */
	@Override
	public RepertoryModel findByPwId(String pwId) {
		// TODO Auto-generated method stub
		return repertoryDao.findByPwId(pwId);
	}
}